package com.deuwise.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.deuwise.common.utils.FileUtil;
import com.deuwise.common.utils.UploadProperties;
import com.deuwise.system.entity.FileDo;
import com.deuwise.system.service.FileDoService;
import com.deuwise.system.mapper.FileDoMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * 文件 接口实现类
 *@author deuwise
 *@date 2018-11-21 11:02:18
 **/
@Service
public class FileDoServiceImpl extends ServiceImpl<FileDoMapper, FileDo> implements FileDoService {

    Logger logger= LoggerFactory.getLogger(FileDoServiceImpl.class);

    @Autowired
    UploadProperties uploadProperties;

    @Override
    public FileDo saveFile(MultipartFile file,String userId) {
        FileDo result=null;
        String originalFileName = file.getOriginalFilename();
        String fileName = FileUtil.renameToUUID(originalFileName);
        FileDo sysFile = new FileDo();
        String fileType1= FileUtil.getFileType(originalFileName);
        originalFileName=originalFileName.replace(fileType1,"");
        sysFile.setName(originalFileName);
        Calendar now = Calendar.getInstance();
        String dayPath=now.get(Calendar.YEAR)+"/"+(now.get(Calendar.MONTH) + 1+"/"+(now.get(Calendar.DATE))) ;
        int count =queryByName(fileName,null);
        if (count > 0) {
            //创建Random类对象
            Random random = new Random();

            //产生随机数
            int number = random.nextInt(99 - 0 + 1) + 0;
           String fileType= FileUtil.getFileType(fileName);
            fileName=fileName.replace(fileType,"");
            if(fileName.length()>27) {
                fileName=fileName.substring(0, 27);
            }
            fileName= fileName+ number+fileType;
        }
        sysFile.setUrl(dayPath+"/"+fileName);

       /* String fileName = FileUtil.renameToUUID(originalFileName);
        FileDo sysFile = new FileDo();
        sysFile.setUrl(fileName);
        int count =queryByName(originalFileName,null);
        if (count > 0) {
            String fileType= FileUtil.getFileType(originalFileName);
            originalFileName=originalFileName.replace(fileType,"");
            originalFileName= originalFileName+ UUID.randomUUID()+fileType;
        }*/
      //  sysFile.setName(originalFileName);
        sysFile.setSizeNum(file.getSize());
        sysFile.setType(file.getContentType());
        sysFile.setUserId(userId);
        sysFile.setCreateDate(new Date());
        try {


            FileUtil.uploadFile(file.getBytes(), uploadProperties.getBasePath()+dayPath+"/", fileName);
        } catch (Exception e) {
            return result;
        }

        if(baseMapper.insert(sysFile)>0){
            result = sysFile;
        }

        return result;
    }

    @Override
    public Integer queryByName(String name, FileDo fileDo) {
        Wrapper<FileDo> dictDoEntityWrapper=new EntityWrapper<>();
        dictDoEntityWrapper.eq("name",name);
        if(fileDo!=null){
            if(StringUtils.isNotBlank(fileDo.getId())) {
                dictDoEntityWrapper.ne("id", fileDo.getId());
            }
        }
        return baseMapper.selectCount(dictDoEntityWrapper);
    }

    @Override
    public int remove(String id) {
        FileDo file = baseMapper.selectById(id);
        int result = baseMapper.deleteById(id);
        if(result>0){
            FileUtil.deleteFile(uploadProperties.getBasePath()+file.getUrl());
        }
        return result;
    }
}